package cn.edu.scujcc;


	import java.util.List;
	import java.util.Optional;

	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.stereotype.Service;

	@Service
	public class UserService {
		@Autowired
		private UserRepository repo;
		
		public List<User> getAllUser(){
			List<User>result = null;
			result = repo.findAll();
			return result;
		}
		public User getUser(String id) {
			User result = null;
			Optional<User> op = repo.findById(id);
			if (op.isPresent()) {
				result = op.get();
				}
			return result;
			}

		public User createDynamic(String dynamic) {
			User result = null;
			Optional<User> op = repo.findById(dynamic);
			if (op.isPresent()) {
				result = op.get();
				}
			return result;
			}
		public User createUser(User u) {
			User result = null;
			result = repo.save(u);
			return result;
		}
		public User updateUser(User u) {
			User result = null;
			result = repo.findById(u.getId()).get();
			if(result !=null) {
			if(u.getName()!=null){
				result.setName(u.getName());
			}
			if(u.getDynamic()!=null){
				result.setDynamic(u.getDynamic());
			}
			if(u.getLike()!=null){
				result.setLike(u.getLike());
			}
			if(u.getGrader()!=null){
				result.setGrader(u.getGrader());
			}
			result = repo.save(result);
			}
			return result;
		}
}
